package com.mylove.judas.yaya.common.rx;

import com.mylove.judas.yaya.common.exception.ApiException;
import com.mylove.judas.yaya.entity.base.ObjectResponse;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * Http响应预处理..
 * Created by Administrator on 2017/5/26 0026.
 */
public class RxHttpResponseCompat {

    public static <T> ObservableTransformer<ObjectResponse<T>, T> compatResult(){

        return new ObservableTransformer<ObjectResponse<T>, T>() {
            @Override
            public ObservableSource<T> apply(@NonNull Observable<ObjectResponse<T>> upstream) {
                return upstream.flatMap(new Function<ObjectResponse<T>, ObservableSource<T>>() {
                    @Override
                    public ObservableSource<T> apply(@NonNull final ObjectResponse<T> tObjectResponse) throws Exception {

                        if (tObjectResponse.isSuccess()) {

//                            tObjectResponse.data;

                            return Observable.create(new ObservableOnSubscribe<T>() {
                                @Override
                                public void subscribe(@NonNull ObservableEmitter<T> emitter) throws Exception {
                                    try{
                                        emitter.onNext(tObjectResponse.data);
                                        emitter.onComplete();
                                    }catch (Exception e){
                                        emitter.onError(e);
                                    }
                                }
                            });

                        }else{
                            return Observable.error(new ApiException(tObjectResponse.code, tObjectResponse.getMessage()));
                        }

//                        return null;
                    }
                }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
            }
        };
    }



    public static ObservableTransformer<ObjectResponse, ?> compatResultObject(){

        return new ObservableTransformer<ObjectResponse, Object>() {
            @Override
            public ObservableSource<Object> apply(@NonNull Observable<ObjectResponse> upstream) {

                return upstream.flatMap(new Function<ObjectResponse, ObservableSource<?>>() {
                    @Override
                    public ObservableSource<?> apply(@NonNull ObjectResponse objectResponse) throws Exception {

                        if(objectResponse.isSuccess()){

                            return Observable.just(1);

                        }else{
                            return Observable.error(new ApiException(objectResponse.code, objectResponse.getMessage()));
                        }

                    }
                }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
            }
        };
    }





















//    public static <T> ObservableTransformer<ObjectResponse<T>, T>compatResult(){
//
//        return new ObservableTransformer<ObjectResponse<T>, T>() {
//            @Override
//            public ObservableSource<T> apply(@NonNull Observable<ObjectResponse<T>> upstream) {
//
//                return upstream.flatMap(new Function<ObjectResponse<T>, ObservableSource<T>>() {
//                    @Override
//                    public ObservableSource<T> apply(@NonNull final ObjectResponse<T> tObjectResponse) throws Exception {
//
//                        if(tObjectResponse.isSuccess()){
//
//                            return Observable.create(new ObservableOnSubscribe<T>() {
//                                @Override
//                                public void subscribe(@NonNull ObservableEmitter<T> emitter) throws Exception {
//
//                                    try{
//                                        emitter.onNext(tObjectResponse.data);
//                                        emitter.onComplete();
//                                    }catch (Exception e){
//                                        emitter.onError(e);
//                                    }
//
//
//                                }
//                            });
//                        }else{
//
//                           return Observable.error(new ApiException(tObjectResponse.code, tObjectResponse.getMessage()));
//                        }
//
//                    }
//                }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
//            }
//        };
//    }

}
